package com.example.smartcity_poverty.Base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity_poverty.logic.model.stop.StopWhereAll

/**
 *  created by ikun
 *  ANDROID STUDIO - version 4.0
 *  email: jinkunwei41@gmail.com
 * --------------------------------
 *  我比赛的时候提供的Android Studio开发工具版本是4.0， 所以是支持kotlin-android-extensions这个插件的
 *
 *
 *  TODO 然后我看了2024年比赛提供的版本已经升级到4.2.2了， 所以这个封装RecyclerView在4.2.2使用不了
 *  TODO 我已经根据ViewBinding来重新写了一个
 *  @see com.example.smartcity_poverty.Base.NewRecyclerAdapter
 *
 **/
abstract class BaseRecyclerAdapter <E>(
    private val list: MutableList<E>,
    @LayoutRes
    private val layoutId: Int
) : RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder>(){

    // 设置数据
    abstract fun setData(view: BaseViewHolder.ViewFind, data: E, position: Int, holder: BaseViewHolder)

    // 填充数据
    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        if (list.size >= 0) {
            setData(holder.viewFind, list[position], position, holder)
        }
    }

    // 列表数量
    override fun getItemCount(): Int = list.size

    // 布局Id
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder
        = BaseViewHolder(LayoutInflater.from(parent.context).inflate(layoutId, parent, false))


    /**
     * 封装ViewHolder
     */
    class BaseViewHolder(private val view: View) : RecyclerView.ViewHolder(view){
        val viewFind = ViewFind().apply { this.setView(this@BaseViewHolder.view) }

        inner class ViewFind : Fragment(){
            private lateinit var mView: View

            fun setView(view: View){
                mView = view
            }
            override fun getView(): View? = mView
        }
    }
}